﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyWebUtils;
using TTQT;
using TTQT.TTQT_Code;
public partial class CN : MasterControl//System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = string.Empty;
        if (!Page.IsPostBack)
        {
            bindGrid();
            visiblePanels(true, false);
            return;
        }
    }
    private void bindGrid()
    {
        try
        {
            ds.SelectCommandType = SqlDataSourceCommandType.Text;
            ds.SelectCommand = "select * from MaKho where makhocaptren='" + UserInfo.MaKho + "' and makho not in (select User_name from users) order by MaKho";
            datagrid.DataSourceID = "ds";
            datagrid.DataBind();
        }
        catch (Exception)
        {
        }
    }
    private void visiblePanels(bool visibleGrid, bool visibleEdit)
    {
        this.pnGroupGrid.Visible = visibleGrid;
        this.pnEdit.Visible = visibleEdit;
    }
    protected void cmdAddNew_Click(object sender, EventArgs e)
    {
        txtMaKho.Enabled = true;
        lblError.Text = "";
        lblUpdateStatus.Text = "";
        visiblePanels(false, true);
        this.txtMaKho.Text = string.Empty;
        this.txtTenKho.Text = string.Empty;
    }

    protected void cmdUpdate_Click(object sender, EventArgs e)
    {
        
        try
        {
            // Nếu là sửa Thì ID sẽ khác rỗng

            if (!txtMaKho.Enabled)
            {
                ds.UpdateCommand = "update MaKho set TenKho=N'" + txtTenKho.Text + "' where MaKho='" + txtMaKho.Text+"'";
                ds.Update();
                //ds.UpdateCommand = "update BrnStatus set branchName=N'" + txtTenKho.Text + "' where BranchCode='" + txtMaKho.Text+"'";
                //ds.Update();
                bindGrid();
                visiblePanels(true, false);
                lblError.Text = "";
            }
            // Nếu là thêm mới thì ID sẽ rỗng
            else
            {
                ds.InsertCommand = "insert into MaKho(MaKho,MaKhoCapTren,TenKho,CapQuanLy,CayPhanCap) values('" + txtMaKho.Text + "','"+UserInfo.MaKho+"',N'" + txtTenKho.Text + "',2,'"+UserInfo.MaKho+"/"+txtMaKho.Text+"')";
                ds.Insert();
                //try
                //{
                //    ds.InsertCommand = "insert into BrnStatus(BranchCode,BranchName,HO_Branch) values('" + txtMaKho.Text + "',N'" + txtTenKho.Text + "','" + UserInfo.Brn_Code + "')";
                //    ds.Insert();
                //}catch(Exception ex){}
                
                bindGrid();
                visiblePanels(true, false);
            }
        }
        catch (Exception ex)
        {
            lblUpdateStatus.Attributes.Add("style", "color:red;font-weight:bold;");
            this.lblUpdateStatus.Text = "Mã cửa hàng này đã tồn tại trong hệ thống. Bạn vui lòng chọn lại! Xin cảm ơn! <br />"+ex.ToString();
            //Log methods here
        }
    }
    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        lblUpdateStatus.Text = "";
        visiblePanels(true, false);
    }
    protected void datagrid_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if (e.CommandName == "cmdEdit")
        {
            txtMaKho.Text = e.Item.Cells[0].Text;
            txtTenKho.Text = e.Item.Cells[1].Text;
            visiblePanels(false, true);
            /*
            ds.SelectCommandType = SqlDataSourceCommandType.Text;
            ds.SelectCommand = "select * from MaKho where MaKho=" + e.Item.Cells[0].Text;
            DataTable hehe = ((DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty)).ToTable();

            hehe.Rows[0][1].ToString();
            this.txtMaKho.Text = hehe.Rows[0][0].ToString();
            this.txtTenKho.Text = hehe.Rows[0][1].ToString();
            */
            // Save old info
            txtMaKho.Enabled = false;
        }
        else if (e.CommandName == "cmdDelete")
        {
            DataView kq;
            ds.SelectCommand = "select count(*) from hoadon_detail where khoxuat='" + e.Item.Cells[0].Text + "' or KhoNhan= '" +
                               e.Item.Cells[0].Text + "'";
            kq = (DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty);
            
            if(!kq.Table.Rows[0][0].ToString().Equals("0"))
            {
                MyWebUtil.alert(this.Page,"Đã có giao dịch xuất (nhập) liên quan nên không xóa được.");
                bindGrid();
                return;
            }

            ds.SelectCommand = "Select count(*) from users where brn_Code='" + e.Item.Cells[0].Text + "'";
            kq = (DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty);

            if (!kq.Table.Rows[0][0].ToString().Equals("0"))
            {
                MyWebUtil.alert(this.Page, "Bạn phải xóa các user thuộc PGD này trước khi xóa PGD");
                bindGrid();
                return;
            }
            ds.DeleteCommand = "delete MaKho where MaKho='" + e.Item.Cells[0].Text+"'";
            ds.Delete();
            ds.DeleteCommand = "delete BrnStatus where BranchCode='" + e.Item.Cells[0].Text + "'";
            ds.Delete();

            bindGrid();
            
        }
    }
    protected void datagrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex < 0) return;
        Button cmdDel = (Button)e.Item.FindControl("cmdDelete");
        if (cmdDel != null) cmdDel.Attributes.Add("onclick", "delete_Confirm");
    }
    protected void datagrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        datagrid.CurrentPageIndex = e.NewPageIndex;
        bindGrid();
    }
}
